﻿using System;
using System.Collections.Generic;
using SR = System.Reflection;

namespace Needle.Core.Reflection
{
    /// <summary>
    /// System.Type Extension Methods
    /// </summary>
    public static class TypeExtensions
    {

        /// <summary>
        /// Returns the list of constructors that can be called with the signature given.
        /// </summary>
        public static SR.ConstructorInfo[] GetConstructors(this Type type, params Type[] paramTypes)
        {
            if (type == null)
                throw new NullReferenceException();

            List<SR.ConstructorInfo> infos = new List<SR.ConstructorInfo>();
            foreach (SR.ConstructorInfo ci in type.GetConstructors())
            {
                if (ci.IsCompatibleSignature(paramTypes))
                    infos.Add(ci);
            }
            return infos.ToArray();
        }

        /// <summary>
        /// Returns the list of constructors that can be called with the signature given.
        /// </summary>
        public static SR.ConstructorInfo[] GetConstructors(this Type type, SR.BindingFlags bindingFlags, params Type[] paramTypes)
        {
            if (type == null)
                throw new NullReferenceException();

            List<SR.ConstructorInfo> infos = new List<SR.ConstructorInfo>();
            foreach (SR.ConstructorInfo ci in type.GetConstructors(bindingFlags))
            {
                if (ci.IsCompatibleSignature(paramTypes))
                    infos.Add(ci);
            }
            return infos.ToArray();
        }

    }

}
